/**
 * @file lv_port_indev_templ.c
 *
 */

/*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/

/*********************
 *      INCLUDES
 *********************/
//#include "lv_port.h"
#include <lvgl.h>
#include <Arduino.h>
/*********************
 *      DEFINES
 *********************/
#define KEY_UP 8    //按键up
#define KEY_DOWN 13 //按键down
#define KEY_LEFT 5  //按键left
#define KEY_RIGHT 9 //按键right
#define KEY_OK 4    //按键ok

#define KEY_UP_PRES 1    // KEY_UP
#define KEY_DOWN_PRES 2  // KEY_DOWN
#define KEY_LEFT_PRES 3  // KEY_LEFT
#define KEY_RIGHT_PRES 4 // KEY_RIGHT
#define KEY_OK_PRES 5    // KEY_OK
/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

static void keypad_init(void);
static void keypad_read(lv_indev_drv_t *indev_drv, lv_indev_data_t *data);
static uint32_t keypad_get_key(void);
static uint32_t KEY_Scan(char mode);

lv_indev_t *indev_keypad;

void lv_port_indev_init(void)
{

    keypad_init();
    static lv_indev_drv_t indev_drv;
    /*Register a keypad input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_KEYPAD;
    indev_drv.read_cb = keypad_read;
    indev_keypad = lv_indev_drv_register(&indev_drv);
    lv_group_t *g = lv_group_create();
    lv_indev_set_group(indev_keypad, g);
    lv_group_set_default(g);
}
static void keypad_init(void)
{
    pinMode(KEY_UP, INPUT);
    pinMode(KEY_DOWN, INPUT_PULLUP);
    pinMode(KEY_LEFT, INPUT_PULLUP);
    pinMode(KEY_RIGHT, INPUT);
    pinMode(KEY_OK, INPUT_PULLUP);
}
static void keypad_read(lv_indev_drv_t *indev_drv, lv_indev_data_t *data)
{
    static uint32_t last_key = 0;

    /*Get the current x and y coordinates*/

    /*Get whether the a key is pressed and save the pressed key*/
    uint32_t act_key = KEY_Scan(1);

    if (act_key != 0)
    {
        data->state = LV_INDEV_STATE_PR;

        /*Translate the keys to LVGL control characters according to your key definitions*/
        switch (act_key)
        {
        case 1:
            act_key = LV_KEY_NEXT;
            break;
        case 2:
            act_key = LV_KEY_PREV;
            break;
        case 3:
            act_key = LV_KEY_LEFT;
            break;
        case 4:
            act_key = LV_KEY_RIGHT;
            break;
        case 5:
            act_key = LV_KEY_ENTER;
            break;
        }

        last_key = act_key;
    }
    else
    {
        data->state = LV_INDEV_STATE_REL;
    }

    data->key = last_key;
}

static uint32_t KEY_Scan(char mode)
{
    static char key_up = 1; //按键松开标志
    if (mode)
        key_up = 1; //支持长按
    if (key_up && (digitalRead(KEY_OK) == 0 || digitalRead(KEY_UP) == 0 || digitalRead(KEY_DOWN) == 0 || digitalRead(KEY_LEFT) == 0 || digitalRead(KEY_RIGHT) == 0))
    {
        delay(10); //去抖动
        key_up = 0;
        if (digitalRead(KEY_OK) == 0)
            return KEY_OK_PRES;
        else if (digitalRead(KEY_UP) == 0)
            return KEY_UP_PRES;
        else if (digitalRead(KEY_DOWN) == 0)
            return KEY_DOWN_PRES;
        else if (digitalRead(KEY_LEFT) == 0)
            return KEY_LEFT_PRES;
        else if (digitalRead(KEY_RIGHT) == 0)
            return KEY_RIGHT_PRES;
    }
    else if (digitalRead(KEY_OK) == 1 && digitalRead(KEY_UP) == 1 && digitalRead(KEY_DOWN) == 1 && digitalRead(KEY_LEFT) == 1 && digitalRead(KEY_RIGHT) == 1)
        key_up = 1;
    return 0;
}
